import axios from 'axios';
import { setCode, getCode } from '../data/code.js';

/**
 * 短信验证码
 * @param {*} mobile 
 */

export const sendCode = async (mobile, ctx) => {
    let status = false
    // 随机生成6位数的验证码
    const code = () => {
        let code = '';
        for (let i = 0; i < 6; i++) {
            code += Math.floor(Math.random() * 10);
        }
        return code;
    }

    const randomCode = code()
    setCode(mobile, randomCode)
    console.log(getCode(mobile));


    const host = 'https://gyytz.market.alicloudapi.com';
    const path = '/sms/smsSend';
    const method = 'post';
    const appcode = 'd37a40799c614e55a309b79104cbba50';

    const headers = {
        Authorization: `APPCODE ${appcode}`
    };

    const params = {
        mobile: mobile,
        param: `**code**:${randomCode},**minute**:5`,
        smsSignId: '2e65b1bb3d054466b82f0c9d125465e2',
        templateId: '908e94ccf08b4476ba6c876d13f084ad'
    };

    await axios({
        method: method,
        url: `${host}${path}`,
        headers: headers,
        params: params
    })
        .then(response => {
            status = true
        })
        .catch(error => {
            status = false
        })


    return status;
};



